/**Map.java
 * 
 * This class is for the Map the user will see on the screen.
 * 
 * @author Christopher Carithers
 * Last Updated: 4/15/2014
 *
 */

import java.io.IOException;
import java.util.List;
import java.util.Vector;

import Utilities.BufferedReader;

public class Map 
{
   private String m_MapFile;
   private int m_NumberOfX;
   private int m_NumberOfY;
   private List<MapSquare> m_MapSquare = new Vector<MapSquare>();
   
   public Map(String mapFile, int numberOfX, int numberOfY)
   {
      m_MapFile = mapFile;
      m_NumberOfX = numberOfX;
      m_NumberOfY = numberOfY;
   }
   
   public void loadMap()
   {
	  char mapCharacterArray[][];
	  char mapCharacter;
      BufferedReader readMapFile = new BufferedReader(m_MapFile, m_NumberOfX, m_NumberOfY);
   	  try 
   	  {
         readMapFile.setCharacterMapArray();
         mapCharacterArray = readMapFile.getCharacterMapArray();
         
  	     int i;
  	     int j;
  	     for(i = 0; i < m_NumberOfY; i++)
  	     {
  	        for(j = 0; j < m_NumberOfX; j++)
  	        {
  			   mapCharacter = mapCharacterArray[i][j];
  			   MapSquare mapSquare = new MapSquare(j+1, i+1, mapCharacter);
  			   m_MapSquare.add(mapSquare);
  	        }
  	     }
	  }
   	  catch(IOException e)
   	  {
		  // TODO Auto-generated catch block
		  e.printStackTrace();
	  }
   }
   
   public void setMapFile(String mapFile)
   {
	   m_MapFile = mapFile;
   }
   
   public String getMapFile()
   {
	   return m_MapFile;
   }
   
   public void printMap()
   {
	   int i;
	   for(i = 0; i < m_MapSquare.size(); i++)
	   {
	      System.out.print(m_MapSquare.get(i).getObjectOnSquare());
		  
	      if(i%m_NumberOfX == 15)
	      {
	         System.out.print("\n");
	      }
	   }
   }
   
   public void placePlayerToMap(int xPosition, int yPosition)
   {
	   int i = 0;
	   int playerX = m_MapSquare.get(i).getXCoordinate();
	   int playerY = m_MapSquare.get(i).getYCoordinate();
	   while((playerX != xPosition) || (playerY != yPosition))
	   {
	      i++;
		  playerX = m_MapSquare.get(i).getXCoordinate();
		  playerY = m_MapSquare.get(i).getYCoordinate();
	   }

	   if(m_MapSquare.get(i).getXCoordinate() == xPosition && m_MapSquare.get(i).getYCoordinate() == yPosition)
	   {
		  m_MapSquare.get(i).setObjectOnSquare('P');
	   }
   }
   
   public void updatePlayerOnMapNorth(int xPosition, int yPosition)
   {
	  int previousYPosition = yPosition + 1;
	  
	  int i= 0;
	  
	  while(m_MapSquare.get(i).getXCoordinate() != xPosition || m_MapSquare.get(i).getYCoordinate() != previousYPosition)
	  {
	     i++;
	  }
	   
	  if(m_MapSquare.get(i).getXCoordinate() == xPosition && m_MapSquare.get(i).getYCoordinate() == previousYPosition)
	  {
		 m_MapSquare.get(i).setObjectOnSquare(' ');
	  }
	  
	  i = 0;
	  while(m_MapSquare.get(i).getXCoordinate() != xPosition || m_MapSquare.get(i).getYCoordinate() != yPosition)
	  {
	     i++;
	  }
	   
	  if(m_MapSquare.get(i).getXCoordinate() == xPosition && m_MapSquare.get(i).getYCoordinate() == yPosition)
	  {
		 m_MapSquare.get(i).setObjectOnSquare('P');
	  }
   }
   
   public void updatePlayerOnMapSouth(int xPosition, int yPosition)
   {
	  int previousYPosition = yPosition - 1;
	  
	  int i = 0;
	  
	  while(m_MapSquare.get(i).getXCoordinate() != xPosition || m_MapSquare.get(i).getYCoordinate() != previousYPosition)
	  {
	     i++;
	  }
	   
	  if(m_MapSquare.get(i).getXCoordinate() == xPosition && m_MapSquare.get(i).getYCoordinate() == previousYPosition)
	  {
		 m_MapSquare.get(i).setObjectOnSquare(' ');
	  }

	  i = 0;
	  while(m_MapSquare.get(i).getXCoordinate() != xPosition || m_MapSquare.get(i).getYCoordinate() != yPosition)
	  {
	     i++;
	  }
	   
	  if(m_MapSquare.get(i).getXCoordinate() == xPosition && m_MapSquare.get(i).getYCoordinate() == yPosition)
	  {
		 m_MapSquare.get(i).setObjectOnSquare('P');
	  }
   }
   
   public void updatePlayerOnMapEast(int xPosition, int yPosition)
   {
	  int previousXPosition = xPosition - 1;
	  
	  int i = 0;
	  
	  while(m_MapSquare.get(i).getXCoordinate() != previousXPosition || m_MapSquare.get(i).getYCoordinate() != yPosition)
	  {
	     i++;
	  }
	   
	  if(m_MapSquare.get(i).getXCoordinate() == previousXPosition && m_MapSquare.get(i).getYCoordinate() == yPosition)
	  {
		 m_MapSquare.get(i).setObjectOnSquare(' ');
	  }

	  i = 0;
	  while(m_MapSquare.get(i).getXCoordinate() != xPosition || m_MapSquare.get(i).getYCoordinate() != yPosition)
	  {
	     i++;
	  }
	   
	  if(m_MapSquare.get(i).getXCoordinate() == xPosition && m_MapSquare.get(i).getYCoordinate() == yPosition)
	  {
		 m_MapSquare.get(i).setObjectOnSquare('P');
	  }
   }
   
   public void updatePlayerOnMapWest(int xPosition, int yPosition)
   {
	  int previousXPosition = xPosition + 1;
	  
	  int i = 0;
	  while(m_MapSquare.get(i).getXCoordinate() != previousXPosition || m_MapSquare.get(i).getYCoordinate() != yPosition)
	  {
	     i++;
	  }
	   
	  if(m_MapSquare.get(i).getXCoordinate() == previousXPosition && m_MapSquare.get(i).getYCoordinate() == yPosition)
	  {
		 m_MapSquare.get(i).setObjectOnSquare(' ');
	  }

	  i = 0;
	  while(m_MapSquare.get(i).getXCoordinate() != xPosition || m_MapSquare.get(i).getYCoordinate() != yPosition)
	  {
	     i++;
	  }
	   
	  if(m_MapSquare.get(i).getXCoordinate() == xPosition && m_MapSquare.get(i).getYCoordinate() == yPosition)
	  {
		 m_MapSquare.get(i).setObjectOnSquare('P');
	  }
   }
   
   public boolean mapSquareIsEmpty(int xPosition, int yPosition)
   {
	  int i = 0;
	  while(m_MapSquare.get(i).getXCoordinate() != xPosition || m_MapSquare.get(i).getYCoordinate() != yPosition)
	  {
		 i++;
	  }
	  if(m_MapSquare.get(i).getObjectOnSquare() == ' ')
	  {
         return true;
	  }
	  else
	  {
		 return false;
	  }
   }
   
   public char objectOnMapSquare(int xPosition, int yPosition)
   {
	  int i = 0;
	  while(m_MapSquare.get(i).getXCoordinate() != xPosition || m_MapSquare.get(i).getYCoordinate() != yPosition)
	  {
		 i++;
	  }
	  if(m_MapSquare.get(i).getObjectOnSquare() == ' ')
	  {
         return ' ';
	  }
	  else
	  {
		 return m_MapSquare.get(i).getObjectOnSquare();
	  }
   }
}
